﻿@model IEnumerable<ArivisTests.Models.TestCase>
@using ArivisTests.Models;
@using ArivisTests.Views;

@functions {
    int GetParameter(string key, int defaultValue)
    {
        int value;
        return (Int32.TryParse(Request[key], out value)) ? value : defaultValue;
    }
    MvcHtmlString OrderLink<TModel, TValue>(HtmlHelper<IEnumerable<TModel>> html, System.Linq.Expressions.Expression<Func<TModel, TValue>> expression, string param)
    {
        MvcHtmlString label = html.DisplayNameFor(expression);
        string label2 = null;
        
        var routeValues = new RouteValueDictionary(this.ViewContext.RouteData.Values);

        var order = Request["order"];
        if (string.IsNullOrEmpty(order)) order = "id";

        if (order == param)
        {
            routeValues["order"] = param + "_desc";
            label2 = "&nbsp;&uarr;";
        }
        else if (order == param + "_desc")
        {
            routeValues["order"] =  param;
            label2 = "&nbsp;&darr;";
        }
        else
        {
            routeValues["order"] = param;
            label2 = string.Empty;
        }

        string orderLink = string.Format("<a href=\"{0}\">{1}{2}</a>", Url.RouteUrl(routeValues), label.ToHtmlString(), label2);
        return MvcHtmlString.Create(orderLink);
    }
    string FilterLink(RouteValueDictionary routeValues, string filter, string value)
    {
        var copy = new RouteValueDictionary(this.ViewContext.RouteData.Values);
        if (string.IsNullOrEmpty(value))
            copy.Remove(filter);
        else
            copy[filter] = value;
        return this.Url.RouteUrl(copy);
    }
}

@{
    int page = GetParameter("page", 0);
    int pageSize = GetParameter("pagesize", 50);
    int currentRow = page * pageSize + 1;

    IEnumerable<ArivisTests.Models.TestCase> items = Model;
    var routeValues = this.ViewContext.RouteData.Values;
    
    // filter by tester
    string tester = Request["tester"];
    if (string.IsNullOrEmpty(tester) == false)
    {
        items = items.Where(test => test.User != null && test.User.UserName == tester);
        routeValues["tester"] = tester;
    }
    
    // filter by result
    string result = Request["result"];
    if (string.IsNullOrEmpty(result) == false)
    {
        if (result == "_none")
        {
            items = items.Where(test => test.Result == null);
        }
        else if (result != "_all")
        {
            items = items.Where(test => test.Result != null && test.Result.Name == result);
        }
        routeValues["result"] = result;
    }

    
    // get order
    string order = Request["order"];
    if (string.IsNullOrEmpty(order)) {
        order = "id";
    }
    switch (order)
    {
        case "tester":
            items = items.OrderBy(x => x.User);
            break;
        case "tester_desc":
            items = items.OrderByDescending(x => x.User);
            break;
        case "result":
            items = items.OrderByDescending(x => x.Result);
            break;
        case "result_desc":
            items = items.OrderBy(x => x.Result);
            break;
        case "id_desc":
            items = items.OrderByDescending(x => x.Name, NaturalSort.Instance);
            break;
        default:
            items = items.OrderBy(x => x.Name, NaturalSort.Instance);
            break;
    }
    routeValues["order"] = order;

    // prepare filter for tester
    IEnumerable<UserProfile> dbUsers = ViewBag.Users;
    var users = dbUsers.ToList().Select(x => new SelectListItem() { 
        Value = FilterLink(routeValues, "tester", x.UserName),
        Text = x.UserName,
        Selected = x.UserName == tester 
    }).ToList();
    users.Insert(0, new SelectListItem() { Value = FilterLink(routeValues, "tester", null) });

    // prepare filter for results
    IEnumerable<TestResult> dbResults = ViewBag.Results;
    var results = dbResults.ToList().Select(x => new SelectListItem() { 
        Value = FilterLink(routeValues, "result", x.Name),
        Text = x.Name,
        Selected = x.Name == result
    }).ToList();
    results.Insert(0, new SelectListItem() { Value = FilterLink(routeValues, "result", "_all"), Text = "[All]", Selected = result == "_all" });
    results.Insert(1, new SelectListItem() { Value = FilterLink(routeValues, "result", "_none"), Text = "[None]", Selected = result == "_none" });
}

<div style="margin-top: 1em;">
    <span>Tester: </span>
    @Html.DropDownList("dd_testers", users, new { @onchange="location = this.value;" })
    <span>Result: </span>
    @Html.DropDownList("dd_results", results, new { @onchange="location = this.value;" } )
</div>

<table class="index-table">
    <tr>
        <th style="width:25px"></th>
        <th style="width:330px;white-space:nowrap;">
            @OrderLink(Html, model => model.Name, "id")
        </th>
        <th style="width:60px;white-space:nowrap;">
            @OrderLink(Html, model => model.User, "tester")
        </th>
        <th style="width:60px;white-space:nowrap;">
            @OrderLink(Html, model => model.Result, "result")
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Comment) / @Html.DisplayNameFor(model => model.Tickets)
        </th>
    </tr>
    
@foreach (var item in items.Skip(page * pageSize).Take(pageSize))
{
    <tr>
        <td style="white-space:nowrap;vertical-align:top;">
            @if (item.Result != null && !string.IsNullOrEmpty(item.Result.Color))
            {
                string color = System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.FromName(item.Result.Color));
                <span style="width:3px;background-color:@color">&nbsp;</span>
            }
            else
            {
                <span style="width:3px;">&nbsp;</span>
            }
                
            @(currentRow++)
        </td>
        <td style="white-space:nowrap;vertical-align:top;">
            @Html.ActionLink(item.Name, "Details", "TestCase", new { id = item.Id}, null)
        </td>
        <td style="white-space:nowrap;vertical-align:top;">
            @Html.DisplayFor(modelItem => item.User.UserName)
        </td>
        <td style="white-space:nowrap;vertical-align:top;">
            @Html.DisplayFor(modelItem => item.Result.Name)
        </td>
        <td>
            <div>@Html.DisplayFor(modelItem => item.Comment)</div>
            <div>@Html.DisplayTickets(item.Scenario.TicketUrl, item.Tickets)</div>
        </td>
    </tr>
}

</table>

@Html.Paging(items.Count(), pageSize, "Details", new  RouteValueDictionary(routeValues))

